package com.example.fangwei.utilsapp.convenientbanner;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import com.ToxicBakery.viewpager.transforms.ABaseTransformer;
import com.ToxicBakery.viewpager.transforms.AccordionTransformer;
import com.ToxicBakery.viewpager.transforms.BackgroundToForegroundTransformer;
import com.ToxicBakery.viewpager.transforms.CubeInTransformer;
import com.ToxicBakery.viewpager.transforms.CubeOutTransformer;
import com.ToxicBakery.viewpager.transforms.DefaultTransformer;
import com.ToxicBakery.viewpager.transforms.DepthPageTransformer;
import com.ToxicBakery.viewpager.transforms.FlipHorizontalTransformer;
import com.ToxicBakery.viewpager.transforms.FlipVerticalTransformer;
import com.ToxicBakery.viewpager.transforms.ForegroundToBackgroundTransformer;
import com.ToxicBakery.viewpager.transforms.RotateDownTransformer;
import com.ToxicBakery.viewpager.transforms.RotateUpTransformer;
import com.ToxicBakery.viewpager.transforms.StackTransformer;
import com.ToxicBakery.viewpager.transforms.ZoomInTransformer;
import com.ToxicBakery.viewpager.transforms.ZoomOutTranformer;
import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.example.fangwei.utilsapp.R;
import com.fangwei.utilsLibrary.customerview.viewpager.ScalePageTransformer;
import com.liaoinstan.springview.widget.SpringView;
import java.util.ArrayList;
import java.util.List;


/**
 * 展示广告栏控件的界面
 */
public class ConvenientBannerActivity extends AppCompatActivity {

    private ConvenientBanner banner;
    private SpringView springView;
    private Button button;
    private List<String> transformerList = new ArrayList<>();
    private List<Integer> bannerUrlList = new ArrayList<>();
    private int i = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_convenient_banner);
        initData();
        initView();
    }

    private void initData() {
        //动画形式
        transformerList.add(DefaultTransformer.class.getSimpleName());
        transformerList.add(AccordionTransformer.class.getSimpleName());
        transformerList.add(BackgroundToForegroundTransformer.class.getSimpleName());
        transformerList.add(CubeInTransformer.class.getSimpleName());
        transformerList.add(CubeOutTransformer.class.getSimpleName());
        transformerList.add(DepthPageTransformer.class.getSimpleName());
        transformerList.add(FlipHorizontalTransformer.class.getSimpleName());
        transformerList.add(FlipVerticalTransformer.class.getSimpleName());
        transformerList.add(ForegroundToBackgroundTransformer.class.getSimpleName());
        transformerList.add(RotateDownTransformer.class.getSimpleName());
        transformerList.add(RotateUpTransformer.class.getSimpleName());
        transformerList.add(StackTransformer.class.getSimpleName());
        transformerList.add(ZoomInTransformer.class.getSimpleName());
        transformerList.add(ZoomOutTranformer.class.getSimpleName());
        //Banner图
        bannerUrlList.add(R.mipmap.banner);
    }

    /**
     *初始化控件
     */
    private void initView() {


        banner = ((ConvenientBanner) findViewById(R.id.bannerView));
        button = ((Button) findViewById(R.id.btn_select));

        /**
         * 切换动画形式
         */
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String transforemerName = transformerList.get(i%14);
                try {
                    Class cls = Class.forName("com.ToxicBakery.viewpager.transforms." + transforemerName);
                    ABaseTransformer transformer = (ABaseTransformer) cls.newInstance();
                    banner.getViewPager().setPageTransformer(true, transformer);
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                } catch (InstantiationException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
                i++;
            }

        });

        banner.getViewPager().setPageTransformer(true,new ScalePageTransformer());//默认的切换动画
        banner.getViewPager().setOffscreenPageLimit(3);

        /**
         * banner 加载图片资源
         */
        banner.setPages(new CBViewHolderCreator<LocalImageHolderView>() {
            @Override
            public LocalImageHolderView createHolder() {
                return new LocalImageHolderView();//这里是加载本地图片,需要加载网络图片时替换成NetWorkImageHolderView 就行
            }
        },bannerUrlList);

    }

    @Override
    protected void onResume() {
        super.onResume();
        banner.startTurning(2000);
    }

    @Override
    protected void onPause() {
        super.onPause();
        banner.stopTurning();
    }
}
